package com.dmtt.web.service.impl;

import com.dmtt.web.common.ExportServiceImpl;
import com.dmtt.web.entity.*;
import com.dmtt.web.mapper.ActivationMapper;
import com.dmtt.web.service.ActivationService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

@Service
public class ActivationServiceImpl implements ActivationService {
    @Autowired
    private ActivationMapper activationMapper;

    @Autowired
    private ExportServiceImpl exportService;

    //AGC稼动状况一览图图表
    @Override
    public List<Activation> activation(String page, String startTime, String endTime, String agvName) {
        Calendar calendar=Calendar.getInstance();
        List<Activation> activationList = new ArrayList<>();
        int count = activationMapper.intAgv(agvName);
        if (page == null || StringUtils.isBlank(page)){
            return null;
        }
        if (page.equals("1")){
            //AGC稼动状况一览图图表，按月查询
            if (startTime != null && endTime != null){
                activationList = activationMapper.findActivationMonth1(agvName, startTime, endTime,count);
            } else if (startTime == null && endTime == null) {
                SimpleDateFormat formatter = new SimpleDateFormat("yyyy");
                String time = formatter.format(calendar.getTime());
                activationList = activationMapper.findActivationMonth2(agvName, time,count);

            }
        }else {
            //AGC稼动状况一览图图表，按小车查询
            if (startTime == null && endTime == null){
                SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
                endTime = formatter.format(calendar.getTime());
                calendar.add(Calendar.MONDAY,0);
                calendar.set(Calendar.DAY_OF_MONTH,1);
                startTime = formatter.format(calendar.getTime());

            }
            activationList = activationMapper.findActivationSingle(agvName,startTime,endTime);
        }
        return activationList;
    }

    @Override
    public ResponseEntity<byte[]> activaExport(String page,String startTime, String endTime,  String agvName) {
        try{
            Calendar calendar=Calendar.getInstance();
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
            List<Activation> Activation = activation(page,startTime,endTime,agvName);
            if (page.equals("1")) {
                List<String> title = new ArrayList<>();
                title.add("月份");
                title.add("工作正常");
                title.add("欠压");
                title.add("请更换电池");
                title.add("控制线路故障");
                title.add("站点丢失");
                title.add("电压低");
                title.add("角度错误");
                title.add("电机故障");
                title.add("安全触边");
                title.add("脱轨");
                title.add("急停");
                title.add("顶升故障");
                title.add("定位信标丢失");
                title.add("转向信标丢失");
                title.add("规划失败");
                title.add("罗盘故障");
                title.add("满线");
                title.add("光电避障传感器触发");
                title.add("内部错误");
                title.add("调度指令超时或中断");
                title.add("执行超时");
                title.add("限位触发");
                title.add("总数");
                int flag = 3;
                return exportService.buildResponseEntity(exportService.export((List<Activation>) Activation,title,flag), "全体AGC稼动状况一览图" + formatter.format(calendar.getTime()) + ".xls");
            } else {
                List<String> title = new ArrayList<>();
                title.add("车辆编号");
                title.add("工作正常");
                title.add("欠压");
                title.add("请更换电池");
                title.add("控制线路故障");
                title.add("站点丢失");
                title.add("电压低");
                title.add("角度错误");
                title.add("电机故障");
                title.add("安全触边");
                title.add("脱轨");
                title.add("急停");
                title.add("顶升故障");
                title.add("定位信标丢失");
                title.add("转向信标丢失");
                title.add("规划失败");
                title.add("罗盘故障");
                title.add("满线");
                title.add("光电避障传感器触发");
                title.add("内部错误");
                title.add("调度指令超时或中断");
                title.add("执行超时");
                title.add("限位触发");
                title.add("总数");
                return exportService.buildResponseEntity(exportService.export((List<Activation>) Activation,title,4), "单台AGC稼动状况一览图" + formatter.format(calendar.getTime()) + ".xls");
            }
        } catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<Activation> faultAccumulation(ActivationVo activationVo) {
        Calendar calendar=Calendar.getInstance();
        String startTime = activationVo.getStartTime();
        String endTime = activationVo.getEndTime();
        List<Activation> activationList = new ArrayList<>();
        int count = activationMapper.intAgv(activationVo.getAgvName());
        if (activationVo.getPage() == null || StringUtils.isBlank(activationVo.getPage())){
            return null;
        }
        if (activationVo.getPage().equals("1")){
            if (startTime != null && endTime != null){
                activationList = activationMapper.faultAccumulationMonth1(activationVo.getAgvName(), startTime, endTime,count);
            } else {
                SimpleDateFormat formatter = new SimpleDateFormat("yyyy");
                String time = formatter.format(calendar.getTime());
                activationList = activationMapper.faultAccumulationMonth2(activationVo.getAgvName(),time,count);
            }
        } else if (activationVo.getPage().equals("2")) {
            if (startTime == null && endTime == null){
                SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
                endTime = formatter.format(calendar.getTime());
                calendar.add(Calendar.MONDAY,0);
                calendar.set(Calendar.DAY_OF_MONTH,1);
                startTime = formatter.format(calendar.getTime());

            }
            activationList = activationMapper.faultAccumulationSingle(startTime,endTime);
        }
        return activationList;
    }

    @Override
    public ResponseEntity<byte[]> faultAccumulationExport(ActivationVo activationVo) {
        try{
            Calendar calendar=Calendar.getInstance();
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
            List<Activation> Activation = faultAccumulation(activationVo);
            if (activationVo.getPage().equals("1")) {
                List<String> title = new ArrayList<>();
                title.add("月份");
                title.add("欠压");
                title.add("请更换电池");
                title.add("控制线路故障");
                title.add("站点丢失");
                title.add("电压低");
                title.add("角度错误");
                title.add("电机故障");
                title.add("安全触边");
                title.add("脱轨");
                title.add("急停");
                title.add("顶升故障");
                title.add("定位信标丢失");
                title.add("转向信标丢失");
                title.add("规划失败");
                title.add("罗盘故障");
                title.add("满线");
                title.add("光电避障传感器触发");
                title.add("内部错误");
                title.add("调度指令超时或中断");
                title.add("执行超时");
                title.add("限位触发");
                title.add("总数");
                return exportService.buildResponseEntity(exportService.export((List<Activation>) Activation,title,5), "全体AGC故障率堆积图" + formatter.format(calendar.getTime()) + ".xls");
            } else {
                List<String> title = new ArrayList<>();
                title.add("车辆编号");
                title.add("欠压");
                title.add("请更换电池");
                title.add("控制线路故障");
                title.add("站点丢失");
                title.add("电压低");
                title.add("角度错误");
                title.add("电机故障");
                title.add("安全触边");
                title.add("脱轨");
                title.add("急停");
                title.add("顶升故障");
                title.add("定位信标丢失");
                title.add("转向信标丢失");
                title.add("规划失败");
                title.add("罗盘故障");
                title.add("满线");
                title.add("光电避障传感器触发");
                title.add("内部错误");
                title.add("调度指令超时或中断");
                title.add("执行超时");
                title.add("限位触发");
                title.add("总数");
                return exportService.buildResponseEntity(exportService.export((List<Activation>) Activation,title,6), "单台AGC故障率堆积图" + formatter.format(calendar.getTime()) + ".xls");
            }
        } catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    //AGC稼动状况推移图表
    @Override
    public List<ChangeGrain> changeGrain(String page, String startTime, String endTime, String agvName) {
        Calendar calendar=Calendar.getInstance();
        List<ChangeGrain> activationList = new ArrayList<>();
        if (page == null || StringUtils.isBlank(page)){
            return null;
        }
        int count = activationMapper.intAgv(agvName);
        if (page.equals("1")){
            //AGC稼动状况推移图表，按月查询
            if (startTime != null && endTime != null){
                activationList = activationMapper.faultChangeGrainMonth1(agvName,startTime,endTime,count);
            } else {
                SimpleDateFormat formatter = new SimpleDateFormat("yyyy");
                String time = formatter.format(calendar.getTime());
                activationList = activationMapper.faultChangeGrainMonth2(agvName,time,count);
            }
        }else {
            //AGC稼动状况推移图表，按小车查询
            if (startTime == null && endTime == null){
                SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
                endTime = formatter.format(calendar.getTime());
                calendar.add(Calendar.MONDAY,0);
                calendar.set(Calendar.DAY_OF_MONTH,1);
                startTime = formatter.format(calendar.getTime());

            }
            activationList = activationMapper.faultChangeGrainSingle(agvName,startTime,endTime);
        }
        return activationList;
    }

    //AGC稼动状况推移图表导出
    @Override
    public ResponseEntity<byte[]> changeGrainExport(String page, String startTime, String endTime, String agvName) {
        try{
            Calendar calendar=Calendar.getInstance();
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
            List<ChangeGrain> Activation = changeGrain(page,startTime,endTime,agvName);
            if (page.equals("1")) {
                List<String> title = new ArrayList<>();
                title.add("月份");
                title.add("实际稼动率");
                title.add("目标稼动率");
                title.add("总数");
                return exportService.buildResponseEntity(exportService.export((List<ChangeGrain>) Activation,title,7), "全体AGC稼动状况推移图" + formatter.format(calendar.getTime()) + ".xls");
            } else {
                List<String> title = new ArrayList<>();
                title.add("车辆编号");
                title.add("实际稼动率");
                title.add("目标稼动率");
                title.add("总数");
                return exportService.buildResponseEntity(exportService.export((List<ChangeGrain>) Activation,title,8), "单台AGC稼动状况推移图" + formatter.format(calendar.getTime()) + ".xls");
            }
        } catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public ResponseEntity<byte[]> agcStatisticsExport(String startTime, String endTime, String agvName) {
        try{
            Calendar calendar=Calendar.getInstance();
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
//            if (startTime.equals("undefined") && endTime.equals("undefined")){
//                startTime = null;
//                endTime = null;
//            }
            List<AgcStatistics> AgcStatistics = activationMapper.AgcStatistic(startTime,endTime,agvName);

            List<String> title = new ArrayList<>();
            title.add("编号");
            title.add("车号");
            title.add("任务名称");
            title.add("AGV状态");
            title.add("开始时间");
            title.add("结束时间");
            title.add("时间总和(s)");
            title.add("站点");
            return exportService.buildResponseEntity(exportService.export((List<AgcStatistics>) AgcStatistics,title,12), formatter.format(calendar.getTime()) + ".xls");

        } catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public PageInfo<AgcStatistics> AgcStatistic(String startTime, String endTime, String agvName,Integer currPage) {
        if (startTime!=null&&endTime!=null){
            startTime = startTime+" 00:00:00";
            endTime = endTime + " 23:59:59";
        }
        PageHelper.startPage(currPage,15);
        List<AgcStatistics> agcStatisticsList = activationMapper.AgcStatistic(startTime,endTime,agvName);
        PageInfo<AgcStatistics> pageInfo = new PageInfo<>(agcStatisticsList);
        return pageInfo;

    }

    //AGC平均稼动率导出
    @Override
    public ResponseEntity<byte[]> agcAverageExport(String page,String startTime,String endTime,String agvName) {
        try{

            Calendar calendar=Calendar.getInstance();
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
            if (page.equals("1")) {
                List<AgcAverage> agcAverage = activationMapper.AgcStatistic1(startTime,endTime);
                List<String> title = new ArrayList<>();
                title.add("编号");
                title.add("车号");
                title.add("日期");
                title.add("平均稼动率");
                title.add("无任务等待");
                title.add("关机");
                title.add("工作正常");
                title.add("装卸货等待");
                title.add("空车行驶");
                title.add("平均不稼动率");
                title.add("欠压");
                title.add("调试");
                title.add("交通管制");
                title.add("请更换电池");
                title.add("控制线路故障");
                title.add("站点丢失");
                title.add("电压低");
                title.add("角度错误");
                title.add("电机故障");
                title.add("安全触边");
                title.add("脱轨");
                title.add("急停");
                title.add("顶升故障");
                title.add("定位信标丢失");
                title.add("转向信标丢失");
                title.add("规划失败");
                title.add("罗盘故障");
                title.add("满线");
                title.add("障碍物停车");
                title.add("内部错误");
                title.add("调度指令超时或中断");
                title.add("执行超时");
                title.add("限位触发");
                return exportService.buildResponseEntity(exportService.export((List<AgcAverage>) agcAverage,title,13),  formatter.format(calendar.getTime()) + ".xls");
            } else {
                List<AgcAverage> agcAverage = activationMapper.AgcStatistic2(startTime,endTime,agvName);
                List<String> title = new ArrayList<>();
                title.add("编号");
                title.add("日期");
                title.add("平均稼动率");
                title.add("无任务等待");
                title.add("关机");
                title.add("工作正常");
                title.add("装卸货等待");
                title.add("空车行驶");
                title.add("平均不稼动率");
                title.add("欠压");
                title.add("调试");
                title.add("交通管制");
                title.add("请更换电池");
                title.add("控制线路故障");
                title.add("站点丢失");
                title.add("电压低");
                title.add("角度错误");
                title.add("电机故障");
                title.add("安全触边");
                title.add("脱轨");
                title.add("急停");
                title.add("顶升故障");
                title.add("定位信标丢失");
                title.add("转向信标丢失");
                title.add("规划失败");
                title.add("罗盘故障");
                title.add("满线");
                title.add("障碍物停车");
                title.add("内部错误");
                title.add("调度指令超时或中断");
                title.add("执行超时");
                title.add("限位触发");
                return exportService.buildResponseEntity(exportService.export((List<AgcAverage>) agcAverage,title,14), formatter.format(calendar.getTime()) + ".xls");
            }
        } catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<AgvList> carList() {
        List<AgvList> agvLists = activationMapper.carList();
        return agvLists;
    }

    @Override
    public List<String> yearCalendar(ActivationVo activationVo) {
        if (activationVo.getArrayTime().size() == 0) {
            activationMapper.deleteYear(null);
            return null;
        }
        for (String s : activationVo.getArrayTime()) {
            YearCalendar yearCalendar = activationMapper.findYear(s);
            if (yearCalendar == null){
                activationMapper.saveYearCalendar(s);
            } else {
                activationMapper.deleteYear(s);
            }
        }
        return activationMapper.yearList();
    }

    @Override
    public List<String> yearCalendarQuery() {
        return activationMapper.yearList();
    }

    @Override
    public List<Activation> faultPassing(ActivationVo activationVo) {
        List<Activation> activationList = new ArrayList<>();
        if (activationVo.getPage() == null || StringUtils.isBlank(activationVo.getPage())){
            return null;
        }
        if (activationVo.getPage().equals("1")){
            activationList = activationMapper.faultPassingMonth(activationVo.getAgvName(),activationVo.getStartTime(),activationVo.getEndTime());
        } else if (activationVo.getPage().equals("2")){
            activationList = activationMapper.faultPassingSingle(activationVo.getStartTime(),activationVo.getEndTime());
        }
        return activationList;
    }

    @Override
    public ResponseEntity<byte[]> faultPassingExport(ActivationVo activationVo) {
        try{
            Calendar calendar=Calendar.getInstance();
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
            List<Activation> Activation = faultPassing(activationVo);
            if (activationVo.getPage().equals("1")) {
                List<String> title = new ArrayList<>();
                title.add("月份");
                title.add("工作正常");
                title.add("欠压");
                title.add("请更换电池");
                title.add("控制线路故障");
                title.add("站点丢失");
                title.add("电压低");
                title.add("角度错误");
                title.add("电机故障");
                title.add("安全触边");
                title.add("脱轨");
                title.add("急停");
                title.add("顶升故障");
                title.add("定位信标丢失");
                title.add("转向信标丢失");
                title.add("规划失败");
                title.add("罗盘故障");
                title.add("满线");
                title.add("光电避障传感器触发");
                title.add("内部错误");
                title.add("调度指令超时或中断");
                title.add("执行超时");
                title.add("限位触发");
                title.add("总数");
                title.add("目标不稼动率");
                return exportService.buildResponseEntity(exportService.export((List<Activation>) Activation,title,9), "全体AGC故障率推移图" + formatter.format(calendar.getTime()) + ".xls");
            } else {
                List<String> title = new ArrayList<>();
                title.add("车辆编号");
                title.add("工作正常");
                title.add("欠压");
                title.add("请更换电池");
                title.add("控制线路故障");
                title.add("站点丢失");
                title.add("电压低");
                title.add("角度错误");
                title.add("电机故障");
                title.add("安全触边");
                title.add("脱轨");
                title.add("急停");
                title.add("顶升故障");
                title.add("定位信标丢失");
                title.add("转向信标丢失");
                title.add("规划失败");
                title.add("罗盘故障");
                title.add("满线");
                title.add("光电避障传感器触发");
                title.add("内部错误");
                title.add("调度指令超时或中断");
                title.add("执行超时");
                title.add("限位触发");
                title.add("总数");
                title.add("目标不稼动率");
                return exportService.buildResponseEntity(exportService.export((List<Activation>) Activation,title,10), "单台AGC故障率推移图" + formatter.format(calendar.getTime()) + ".xls");
            }
        } catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public ResponseEntity<byte[]> movementRateExport(ActivationVo activationVo) {
        try{
            List<MovemontRate> movementRate = movementRate(activationVo);
            Calendar calendar=Calendar.getInstance();
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
            if (activationVo.getPage().equals("1")) {
                List<String> title = new ArrayList<>();
                title.add("月份");
                title.add("稼动率");
                title.add("不稼动率");
                title.add("总数");
                return exportService.buildResponseEntity(exportService.export((List<MovemontRate>) movementRate,title,1), "全体AGC稼动率一览表" + formatter.format(calendar.getTime()) + ".xls");
            } else {
                List<String> title = new ArrayList<>();
                title.add("车辆编号");
                title.add("稼动率");
                title.add("不稼动率");
                title.add("总数");
                return exportService.buildResponseEntity(exportService.export((List<MovemontRate>) movementRate,title,2), "单台AGC稼动率一览表" + formatter.format(calendar.getTime()) + ".xls");
            }
        } catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<MovemontRate> movementRate(ActivationVo activationVo) {
        Calendar calendar=Calendar.getInstance();
        List<MovemontRate> movemontRateList = new ArrayList<>();
        int count = activationMapper.intAgv(activationVo.getAgvName());
        if (activationVo.getPage() == null || StringUtils.isBlank(activationVo.getPage())){
            return null;
        }
        if (activationVo.getPage().equals("1")){
            if (activationVo.getStartTime() != null && activationVo.getEndTime() != null){
                movemontRateList = activationMapper.findMovemontMonth1(activationVo.getAgvName(),activationVo.getStartTime(),activationVo.getEndTime(),count);
            } else {
                SimpleDateFormat formatter = new SimpleDateFormat("yyyy");
                String time = formatter.format(calendar.getTime());
                movemontRateList = activationMapper.findMovemontMonth2(activationVo.getAgvName(),time,count);
            }
        } else {
            String startTime = activationVo.getStartTime();
            String endTime = activationVo.getEndTime();
            //AGC稼动状况一览图图表，按小车查询
            if (activationVo.getStartTime() == null && activationVo.getEndTime() == null){
                SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
                endTime = formatter.format(calendar.getTime());
                calendar.add(Calendar.MONDAY,0);
                calendar.set(Calendar.DAY_OF_MONTH,1);
                startTime = formatter.format(calendar.getTime());

            }
            movemontRateList = activationMapper.findMovemontSingle(startTime,endTime);
        }
        return movemontRateList;
    }

}
